package ru.marketsoft.reminder.frames;

import ru.marketsoft.reminder.tools.Settings;
import ru.marketsoft.reminder.tools.Utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * User: dimka3210
 * Date: 01.03.13
 * Time: 22:48
 */
public class SetTime extends JDialog {
    private JComboBox hoursBox1;
    private JComboBox minutesBox2;
    private JButton installButton;
    private JPanel content;
    private JPanel mainPanel;
    private JLabel hoursLabel;
    private JLabel minutes;
    private static SetTime instance = null;

    public SetTime() throws IOException {
        super();
        setContentPane(mainPanel);
        setTitle("Установить время");
        setResizable(false);
        setIconImage(Utils.getIcon());
        pack();

        Dimension size = getSize();
        Dimension useScreen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((int)(useScreen.getWidth()/2-size.getWidth()/2), (int)(useScreen.getHeight()/2-size.getHeight()/2));
        hoursBox1.setSelectedIndex(Utils.getReminderHours(1));
        minutesBox2.setSelectedIndex(Utils.getReminderHours(2));

        installButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int hours = Integer.parseInt(hoursBox1.getSelectedItem().toString());
                int minutes = Integer.parseInt(minutesBox2.getSelectedItem().toString());

                Settings.getInstance().setProperty("time.hours", String.valueOf(hours));
                Settings.getInstance().setProperty("time.minutes", String.valueOf(minutes));

                setVisible(false);
            }
        });

    }

    public static SetTime getInstance(){
        if (instance == null){
            try {
                instance = new SetTime();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return instance;
    }

    public void setVisible(boolean state){
        try {
            /*hoursBox1.setSelectedIndex(Utils.getReminderHours(1));
            minutesBox2.setSelectedIndex(Utils.getReminderHours(2));*/
            hoursBox1.setSelectedItem(Settings.getInstance().getProperty("time.hours"));
            minutesBox2.setSelectedItem(Settings.getInstance().getProperty("time.minutes"));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            super.setVisible(state);
        }
    }
}
